package com.example.video

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.AddEntity
import com.example.base.entity.CommentEntity
import com.example.base.entity.GoodsEntity
import com.example.base.entity.XqEntity
import com.example.base.mvvm.BaseModel
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class XqViewModel:BaseViewModel() {
    //返回值--详情页面
    val xqSuccess = MutableLiveData<BaseModel<XqEntity>>()
    val xqFail = MutableLiveData<String>()
    fun xq(id:Int,type:String){
        viewModelScope.launch {
            try {
                xqSuccess.value = apiServer.xq(id, type)
            }catch (e:Exception){
                xqFail.value = e.message
            }
        }
    }
    //返回值--热门推荐
    val tjSuccess = MutableLiveData<BaseModel<GoodsEntity>>()
    val tjFail = MutableLiveData<String>()
    fun tuijian(limit:Int,page:Int){
        viewModelScope.launch {
            try {
                tjSuccess.value = apiServer.tuijian(limit, page)
            }catch (e:Exception){
                tjFail.value = e.message
            }
        }
    }
    //返回值--添加购物车
    val addSuccess = MutableLiveData<BaseModel<AddEntity>>()
    val addFail = MutableLiveData<String>()
    fun addCar(cartNum:Int,productAttrUnique:String,productId:Int){
        viewModelScope.launch {
            try {
                val map = mutableMapOf<String,Any>()
                map["cartNum"] = cartNum
                map["productAttrUnique"] = productAttrUnique
                map["productId"] = productId
                addSuccess.value = apiServer.addCar(mapToBody(map))
            }catch (e:Exception){
                addFail.value = e.message
            }
        }
    }

    //返回值 ---评论列表
    val commentSuccess = MutableLiveData<BaseModel<CommentEntity>>()
    val commentFail = MutableLiveData<String>()
    fun comment(id:Int,limit:Int,page:Int,type:String){
        viewModelScope.launch {
            try {
                commentSuccess.value = apiServer.comment(id, limit, page, type)
            }catch (e:Exception){
                commentFail.value = e.message
            }
        }
    }

}